package controller;

import dao.RepertoryDao;
import dao.godownDao;
import dao.godownDetailsDao;
import entity.Godown;
import entity.Godowndetails;
import entity.Repertory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.jinhuo;
import service.OkgodownDetailsDao;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/godowns")
public class godowns {
    @Autowired
    private godownDao godownDao;
    @Autowired
    private godownDetailsDao godownDetailsDao;
    @Autowired
    private RepertoryDao repertoryDao;
    @Autowired
    private OkgodownDetailsDao okgodownDetailsDao;
    /**
     * 查询详情页面的信息
     */
    @RequestMapping("/showGoDetails")
    @ResponseBody
    public Map<String,Object>showGoDetails(String seat,int offset,int limit){
        return showGoDetailss(seat,offset,limit);
    }
    public Map<String,Object>showGoDetailss(String seat,int offset,int limit){
        Map<String,Object> map=new HashMap<>();
        if(seat!=null&&seat!=""){
            map.put("rows",godownDetailsDao.showGo(seat,offset,limit));
            map.put("total",godownDetailsDao.showTotalGo(seat));
        }
        return map;
    }
    /**
     * 查询入库单审核的信息
     */
    @RequestMapping("/showGodown")
    @ResponseBody
    public Map<String,Object>showGodown(String seat,int stateId,int offset,int limit){
        Map<String,Object> map=new HashMap<>();
            map.put("rows",godownDao.showGoDown(seat,offset,limit,stateId));
            map.put("total",godownDao.showTotalGoDown(seat,stateId));
        return map;
    }
    /**
     * 修改入库详情单实到数量和实付金额
     */
    @RequestMapping("/upGoGdNums")
    @ResponseBody
    public Map<String,Object>upGoGdNums(String seat,int gdId,double price,int gdNums){
        godownDetailsDao.upgdnums(gdId,gdNums,price);
        return showGoDetailss(seat,0,20);
    }
    /**
     * 统计该单号下的实到数量和实付金额
     */
    @RequestMapping("/total")
    @ResponseBody
    public jinhuo total(String seat){
        jinhuo jinhuo=godownDetailsDao.totalCount(seat);
        return jinhuo;
    }
    /**
     * 统计该单号下的实到数量和实付金额
     */
    @RequestMapping("/upbeizhu")
    @ResponseBody
    public Map<String,Object>  upbeizhu(String seat,String beizhu,int gdId,int offset,int limit){
        godownDetailsDao.upbeizhu(beizhu,gdId);
        return showGoDetailss(seat,offset,limit);
    }
    /**
     * 判断进价和上次进价是否冲突
     */
    @RequestMapping("/flag")
    @ResponseBody
    public boolean flag(String seat){
       boolean flag=false;
        List<Godowndetails> list=godownDetailsDao.showGos(seat);
        for (Godowndetails godowndetails:list){
            Repertory repertory=new Repertory();
            repertory.setIptypeid(godowndetails.getShop().getIptypeid());
            repertory.setFirmId(godowndetails.getFirm().getFirmId());
            repertory.setModel(godowndetails.getShop().getShopType());
            repertory.setRepertoryName(godowndetails.getShop().getShopName());
            repertory.setRepertoryColor(godowndetails.getShop().getShopcolor());
          Repertory repertory1= repertoryDao.flagrepertory(repertory);
          if(repertory1!=null){
              System.out.println(godowndetails.getShop().getShopprice()+"=="+repertory1.getPrice());
              if(godowndetails.getShop().getShopprice()!=repertory1.getPrice()){
                  flag=true;
                  break;
              }
          }
        }

       return flag;
    }

    /**
     * 入库审核
     * @param seat
     * @return
     */
    @RequestMapping("/shenhe")
    @ResponseBody
    public Map<String,Object>shenhe(String seat,String remark, HttpServletRequest request){
        okgodownDetailsDao.shenhe(seat,remark,request);
        HashMap<String,Object> map=new HashMap<>();
        return map;
    }
    /**
     * 入库单据查询
     */
    @RequestMapping("/danju")
    @ResponseBody
    public Map<String,Object> danju(String seat,int stateId,int offset,int limit,String times,String lasttime){
        Map<String,Object> map=new HashMap<>();
        map.put("rows",godownDao.showGoDowns(seat,offset,limit,stateId,times,lasttime));
        map.put("total",godownDao.showTotalGoDowns(seat,stateId,times,lasttime));
        return map;
    }
    /**
     * 入库单据详情信息查询
     */
    @RequestMapping("/detailsDanju")
    @ResponseBody
    public Map<String,Object> detailsDanju(String seat,int offset,int limit){
        Map<String,Object> map=new HashMap<>();
        if(seat==null){
            Godown godown=godownDao.showGoDownss(1);
            map.put("rows",godownDetailsDao.showGo(godown.getSeat(),offset,limit));
        }else{
            map.put("rows",godownDetailsDao.showGo(seat,offset,limit));
        }
        map.put("total",godownDetailsDao.showTotalGo(seat));
        return map;
    }
}
